#include "mymodbus.h"
#include "ui_mymodbus.h"

MyModbus::MyModbus(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MyModbus)
{
    ui->setupUi(this);
    connect(ui->pushButton_5,SIGNAL(clicked()),this,SLOT(btnClickSlot()));
    connect(ui->lineEdit_ac_i,SIGNAL(editingFinished()),this,SLOT(lineClickedSlot()));
    connect(ui->lineEdit_tem,SIGNAL(editingFinished()),this,SLOT(lineClickedSlot()));
    connect(ui->lineEdit_hum,SIGNAL(editingFinished()),this,SLOT(lineClickedSlot()));
    connect(ui->lineEdit_light,SIGNAL(editingFinished()),this,SLOT(lineClickedSlot()));
    connect(ui->lineEdit_tem_m,SIGNAL(editingFinished()),this,SLOT(lineClickedSlot()));
    connect(ui->lineEdit_water,SIGNAL(editingFinished()),this,SLOT(lineClickedSlot()));
    data = new DataInfo;
}

MyModbus::~MyModbus()
{
    delete ui;
}

void MyModbus::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap map(":/new/prefix1/res/1.JPG");
    painter.drawPixmap(0,0,this->width(),this->height(),map);
}


void MyModbus::keyValueGetSlot(int key, QString val)
{
    switch (key) {
    case 305:
        ui->lineEdit_ac_i->setText(val);
        break;
    case 300:
        ui->lineEdit_tem->setText(val);
        break;
    case 301:
        ui->lineEdit_hum->setText(val);
        break;
    case 302:
        ui->lineEdit_light->setText(val);
        break;
    case 303:
        ui->lineEdit_tem_m->setText(val);
        break;
    case 304:
        ui->lineEdit_water->setText(val);
        break;
    }
}

void MyModbus::btnClickSlot()
{
    QJsonObject json;
    json.insert("type",1);
    data->publishMessage(json);

}

void MyModbus::lineClickedSlot()
{
    QJsonObject json;
    QJsonObject ctljson;
    json.insert("type",2);
    if(sender()==ui->lineEdit_tem)
    {
        ctljson.insert("key",300);
        ctljson.insert("val",ui->lineEdit_tem->text());
    }
    if(sender()==ui->lineEdit_hum)
    {
        ctljson.insert("key",301);
        ctljson.insert("val",ui->lineEdit_hum->text());
    }
    if(sender()==ui->lineEdit_light)
    {
        ctljson.insert("key",302);
        ctljson.insert("val",ui->lineEdit_light->text());
    }
    if(sender()==ui->lineEdit_tem_m)
    {
        ctljson.insert("key",303);
        ctljson.insert("val",ui->lineEdit_tem_m->text());
    }
    if(sender()==ui->lineEdit_water)
    {
        ctljson.insert("key",304);
        ctljson.insert("val",ui->lineEdit_water->text());
    }
    if(sender()==ui->lineEdit_ac_i)
    {
        ctljson.insert("key",305);
        ctljson.insert("val",ui->lineEdit_ac_i->text());
    }
    json.insert("data",ctljson);
    data->publishMessage(json);
}


